#!/usr/bin/env perl

BEGIN {
	push @::INC,".." if -f "../pv3.pm";
	push @::INC,$::ENV{CODEMGR_LIBS} if exists($::ENV{CODEMGR_LIBS});
}

use pv3;
use Getopt::Long;
use File::Basename;
use Data::Dumper;
use POSIX qw(strftime);
use strict;
use Config;

$::ARG_pv=undef;
$::ARG_version=undef;
$::ARG_long=undef;
$::ARG_verbose=0;

sub usage {
	print "
Usage: pv3_info [-l] [-p DIR] [-v X] file ...

The pv3_info commands should information about
a particular file (or version) that is under PV3 code
control.

It supports the following command line arguments:

-l  Long output - show information is long format (shows more
    fields, spread across multiple lines).
-p  By default the file in question containing the versions is
    thought to exist in the current directory. Use this option
    to reference the version file kept elsewhere.
-v  Just show information for a particular version number.

If no files are given it will provide all information for all
files currently under code control in the specified code repository.

Written by Simon Edwards, (C) 2006-2010 www.advantsys.co.uk
";
	exit 1;
}

if(!GetOptions(
	"long"		=> \$::ARG_long,
	"pv=s"		=> \$::ARG_pv,
	"version=s"	=> \$::ARG_version,
	)) {
	usage;
}
%::args=();

if($Config{osname} =~ /mswin/i) {
	my @F=();
	for my $cpat (@ARGV) {
		push @F,glob($cpat);
	}
	@ARGV=@F;
}

if(defined($::ARG_pv)) {
	if(! -d dirname($::ARG_pv)) {
		mkdir dirname($::ARG_pv),0775 or die("Failed to create directory $::ARG_pv: $!");
	}
}

if(defined($::ARG_version)) {
	$::args{VERSION}=$::ARG_version;
}
$::args{PVFILE}=$::ARG_pv if defined($::ARG_pv);

{
	my $ref=pv3::info(FILE => [@::ARGV],%::args);
	my $ref2=pv3::info2(FILE => [@::ARGV],%::args);
	my %map_results=();
	my %bad=();
	for my $cref (@$ref) {
		if($cref->[0]!=0) {
			print "Error: $cref->[1]\n";
			if(defined($cref->[2])) {
				$bad{$cref->[2]}=1;
			}
			next;
		}
		$map_results{$cref->[1]->{name}}=[$cref->[1]];
	}
	for my $cref (@$ref2) {
		if($cref->[0]!=0) {
			if(defined($cref->[2]) && exists($bad{$cref->[2]})) {
				next;
			}
			print "Error: $cref->[1]\n";
			next;
		}
		if(!exists($map_results{$cref->[1]->{name}})) {
			next;
		}
		push @{$map_results{$cref->[1]->{name}}},$cref->[1];
	}
	my $vv;
	for my $cfile (sort keys(%map_results)) {
		print "$cfile:\n";
		my @kl=grep {$_ ne "name"} (keys %{$map_results{$cfile}->[0]});
		for my $ckey (sort {int($a) <=> int($b)} (@kl)) {
			my $c=$map_results{$cfile}->[0]->{$ckey};
			my $c2=$map_results{$cfile}->[1]->{$ckey};
			my ($nv,$sha1,$author,$date,$tags,$comment,$osize)=(
				$ckey,$c->{sha1},$c->{author},$c->{date},
				$c->{tags},$c->{comment},$c->{osize});

			my ($type,$sz)=($c2->{type},$c2->{size});
			my @T=split(/\|/,$tags);
			if(@T > 3) {
				$vv="$T[0],$T[1],$T[2]";
			} else {
				$vv=join(",",@T);
			}
			if(!$::ARG_long) {
				printf("%s,$nv %-20s %s\n",$sha1,"[$vv]",$comment);
			} else {
				printf("  Version       : %s,%d\n",$sha1,$nv);
				printf("  Type          : %-10s\n",$type);
				printf("  Original Size : %d\n",$osize);
				printf("  Change Size   : %d\n",$sz);
				printf("  Author        : %s\n",$author);
				printf("  Checked in    : %s\n",strftime("%c",localtime($date)));
				printf("  Tags          : %s\n",$vv);
				my @ll=split(/\n/,$comment);
				printf("  Comments      : $_\n") foreach(@ll);
			}
		}
	}
}

exit(0);
